package com.gt.part

import com.gt.SCUtil
import org.apache.spark.{Partitioner, SparkContext}
import org.apache.spark.rdd.RDD

object Partitioner_0 {

  def main(args: Array[String]): Unit = {
    val sc: SparkContext = SCUtil.buildLocalSc()
    val rdd: RDD[(String, String)] = sc.makeRDD(List(
      ("a", "a"),
      ("b", "a"),
      ("c", "a"),
      ("a", "a")
    ))

    val rdd2: RDD[(String, String)] = rdd.partitionBy(new MyPartitioner())
    rdd2.saveAsTextFile("data/data2")

    sc.stop()
  }


  class MyPartitioner extends Partitioner {
    override def numPartitions: Int = 3

    override def getPartition(key: Any): Int = {
      key match {
        case "a" => 0
        case "b" => 1
        case _ => 2
      }
    }
  }


}
